const express = require('express')
const passport = require('passport')
const router = express.Router()

const Student = require('../../../models/Student')
const Teacher = require('../../../models/Teacher')
const Course = require('../../../models/Course')
const getCurTime = require('../../../utils/getCurTime')

// 测试接口
router.get('/', (req, res) => {
  res.send('hello student')
})


/**
 * todo 给教师留言
 * @description student send leavewords to course teacher
 * @router /api/student/leaveWords
 * @method POST
 */
router.post('/leaveWords', (req, res) => {
  const data = req.body
  Student.findOne({
      studentId: data.studentId
    })
    .then(student => {
      let hasLeavewordsToTeacher = null
      let index = 0

      console.log('111')

      for (let i = 0; i < student.leaveWords.length; i++) {
        if (student.leaveWords[i].id === data.teacherId) {
          console.log('222')
          hasLeavewordsToTeacher = true
          index = i
          break
        }
      }

      if (hasLeavewordsToTeacher) {
        student.leaveWords[index].words.push({
          content: data.leaveWords,
          date: getCurTime()
        })
        console.log('333')
        student.save()
          .then(student => {
            Teacher.findOne({
                teacherId: data.teacherId
              })
              .then(teacher => {
                console.log('444')
                let curIndex = 0
                let hasLeavewordsFromStudent = null
                for (let i = 0; i < teacher.leaveWords.length; i++) {
                  if (teacher.leaveWords[i].id === data.studentId) {
                    curIndex = i
                    hasLeavewordsFromStudent = true
                    break
                  }
                }

                if (hasLeavewordsFromStudent) {
                  teacher.leaveWords[curIndex].words.push({
                    content: data.leaveWords,
                    date: getCurTime()
                  })
                  console.log('555')
                  teacher.save()
                    .then(teacher => {
                      console.log('666')
                      return res.send({
                        status: 1,
                        msg: '留言成功',
                        data: teacher.leaveWords[curIndex]
                      })
                    })
                    .catch(err => {
                      throw err
                    })
                }
              })
              .catch(err => {
                throw err
              })
          })
          .catch(err => {
            throw err
          })

      } else {
        console.log('777')
        student.leaveWords.push({
          id: data.teacherId,
          words: [{
            content: data.leaveWords,
            date: getCurTime()
          }]
        })
        console.log('888')
        student.save()
          .then(student => {
            Teacher.findOne({
                teacherId: data.teacherId
              })
              .then(teacher => {
                console.log('999')
                teacher.leaveWords.push({
                  id: data.studentId,
                  words: [{
                    leaveWords: data.leaveWords,
                    date: getCurTime()
                  }]
                })
                teacher.save()
                  .then(teacher => {
                    console.log('-------')
                    return res.send({
                      status: 1,
                      msg: '留言成功',
                      data: teacher.leaveWords[teacher.leaveWords.length - 1]
                    })
                  })
                  .catch(err => {
                    throw err
                  })
              })
              .catch(err => {
                throw err
              })
          })
          .catch(err => {
            throw err
          })
      }
    })
})

module.exports = router